//
//  YTFindPasswordViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/8.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTFindPasswordViewController.h"
#import "YTFindePasswordViewModel.h"

@interface YTFindPasswordViewController ()

@property (weak, nonatomic) IBOutlet UIButton *areaCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumText;
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UITextField *passwordSureText;
@property (weak, nonatomic) IBOutlet UIButton *resetBtn;
@property (weak, nonatomic) IBOutlet UIButton *verfiyCodeBtn;
@property (weak, nonatomic) IBOutlet UILabel *tipsText;


@property (strong, nonatomic) YTFindePasswordViewModel *viewModel;
@end

@implementation YTFindPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)dealloc {}

- (void)initialize {
    @weakify(self);
    [[self rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id x) {
        @strongify(self);
        [self.navigationController setNavigationBarHidden:NO animated:x];
        [self yt_changStatusBarWithStyle:UIStatusBarStyleDefault];
    }];
    
    self.viewModel = [[YTFindePasswordViewModel alloc] init];
}

- (void)bindViewModel {
    
    RAC(_viewModel, phoneNum) = _phoneNumText.rac_textSignal;
    RAC(_viewModel, password) = _passwordText.rac_textSignal;
    RAC(_viewModel, verifyCode) = _verifyCodeText.rac_textSignal;
    RAC(_viewModel, passwordSure) = _passwordSureText.rac_textSignal;
    RAC(_viewModel, areaCode) = RACObserve(_areaCodeBtn.titleLabel, text);
    _verfiyCodeBtn.rac_command = _viewModel.sendVerifyCommand;
    _resetBtn.rac_command = _viewModel.resetCommand;
    
    UIColor *(^validateColor)(NSNumber *) = [NSObject yt_btnNormalColor:[UIColor blackColor] disabled:[UIColor redColor]];
    RAC(_phoneNumText, textColor) = [[_viewModel.phoneValidate distinctUntilChanged] map:validateColor];
    RAC(_passwordText, textColor) = [[_viewModel.passwordValidate distinctUntilChanged] map:validateColor];
    RAC(_verifyCodeText, textColor) = [[_viewModel.verifyValidate distinctUntilChanged] map:validateColor];
    RAC(_passwordSureText, textColor) = [[_viewModel.passwordSureSignal distinctUntilChanged] map:validateColor];
    
    UIColor *(^btnColorValidate)(NSNumber *) = [NSObject yt_btnNormalColor:kMainBlueColor disabled:[UIColor lightGrayColor]];
    RAC(_verfiyCodeBtn, backgroundColor) = [[_viewModel.phoneValidate distinctUntilChanged] map:btnColorValidate];
    RAC(_resetBtn, backgroundColor) = [[_viewModel.allValidate distinctUntilChanged] map:btnColorValidate];
    
    @weakify(self);
    [[_viewModel.sendVerifyCommand.executionSignals switchToLatest]
     subscribeNext:^(NSString *title) {
         @strongify(self);
         [self.verfiyCodeBtn setTitle:title forState:UIControlStateNormal];
     }];
    
    [[_viewModel.tipsHidden distinctUntilChanged] subscribeNext:^(NSNumber *isShow) {
        @strongify(self);
        self.tipsText.hidden = isShow.boolValue;
    }];
    
    [[_viewModel.resetCommand.executionSignals switchToLatest] subscribeNext:^(NSDictionary *response) {
        @strongify(self);
        if ([response[@"resultCode"] integerValue] == 1000) {
            [self.resetSuccess sendNext:RACTuplePack(self.phoneNumText.text, self.passwordText.text)];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

@end
